#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<queue>
#include<vector>
#include<cstring>
using namespace std;
const int N = 1e4 + 10, INF = 2147483647;
int n, m, s;
typedef pair<int, int> pii;
vector<pii> edges[N];
int dist[N];
bool st[N];
void spfa()
{
    for (int i = 0; i <= n; i++)
    {
        dist[i] = INF;
    }
    dist[s] = 0;
    queue<int> q;
    q.push(s);
    st[s] = true;
    while (q.size())
    {
        int a = q.front();
        q.pop();
        st[a] = false;
        for (auto& e : edges[a])
        {
            int v = e.first;
            int w = e.second;
            if (dist[a] + w < dist[v])
            {
                dist[v] = dist[a] + w;
                if (!st[v])
                {
                    q.push(v);
                    st[v] = true;
                }
            }
        }
    }
}
int main()
{
    cin >> n >> m >> s;
    for (int i = 1; i <= m; i++)
    {
        int u, v, w;
        cin >> u >> v >> w;
        edges[u].push_back({ v,w });
    }
    spfa();
    for (int i = 1; i <= n; i++)
    {
        cout << dist[i] << " ";
    }
    return 0;
}